package com.itheima.controller;

import com.alibaba.dubbo.config.annotation.Reference;
import com.itheima.constant.MessageConst;
import com.itheima.entity.PageResult;
import com.itheima.entity.QueryPageBean;
import com.itheima.entity.Result;
import com.itheima.pojo.Permission;
import com.itheima.service.PermissionService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;
import java.util.Set;

@RestController
@Slf4j
@RequestMapping("/permission")
/**
 *角色组的方式来管理权限
 * 1.管理员组 权限有增删改查  检查项,检查组,套餐
 * 2.用户组   权限只有查     套餐
 *
 */
public class PermissionController {


    @Reference
    PermissionService permissionService;

    //根据角色查询权限
    @RequestMapping("/findByRoleId")
    public Result findByRoleId(Integer roleId){
        Set<Permission> permissions = permissionService.findPermissionsByRoleId(roleId);
        return new Result(true, MessageConst.ACTION_SUCCESS,permissions);
    }
    //增加权限
    @RequestMapping("/add")
    public Result add(@RequestBody Permission permission){
        permissionService.add(permission);
        return new Result(true, MessageConst.ACTION_SUCCESS);
    }
    //删除权限
    @RequestMapping("/del")
    public Result del(Integer id){
        permissionService.del(id);
        return new Result(true, MessageConst.ACTION_SUCCESS);
    }
    //更改权限
    @RequestMapping("/edit")
    public Result update(@RequestBody Permission permission){
        permissionService.update(permission);
        return new Result(true, MessageConst.ACTION_SUCCESS);
    }
    //查询所以权限
    @RequestMapping("/findAll")
    public Result findAll(){
        List<Permission> list = permissionService.findAll();
        return new Result(true, MessageConst.ACTION_SUCCESS,list);
    }
    @RequestMapping("/findByPage")

    public Result findByPage(@RequestBody QueryPageBean queryPageBean){
        PageResult pageResult = permissionService.findByPage(queryPageBean);
        log.debug(pageResult.toString());

        return new Result(true, MessageConst.QUERY_CHECKITEM_SUCCESS, pageResult);
    }
    @RequestMapping("/findById")
    public Result findById(Integer id){
        Permission permission  = permissionService.findById(id);
        return  new Result(true, MessageConst.QUERY_CHECKITEM_SUCCESS,permission);
    }

}
